<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CoOrder;
use App\Admin\Repositories\CoSchoolBuilding;
use App\Models\CoSchool;
use App\Models\CoUser;
use App\Models\CoUserCity;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class CoOrderController extends AdminController
{
    protected $title = '订单管理';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CoOrder(['users']), function (Grid $grid) {
            $grid->model()->orderBy('created_at', 'desc');
            $grid->id->sortable();
            $grid->order_no;
            $grid->column('users.nickname','用户昵称');
            $grid->school_id->display(function ($school_id){
                return CoSchool::where('id',$school_id)->value('name');
            });
            $grid->column('distribution_type','配送方式')->display(function ($type){
                if($type == 1){
                   return '配送上楼';
                }else{
                    return '配送至楼下';
                }
            });
            $grid->column('distribution_time','配送时间');
            $grid->column('is_urgent','是否加急')->display(function ($is_urgent){
                if($is_urgent == 1){
                    return "<span style='color:red'>加急</span>";
                }else{
                    return "<span style='color:blue'>普通</span>";
                }
            });
            $grid->status->display(function ($status){
                switch ($status){
//                订单状态（0待支付, 1已支付/待接单, 2已接单/待取货, 3服务中, 4已完成，5已取消, 6已关闭）
                    case 0:
                        return "<span style='color:blue'>待支付</span>";
                    case 1:
                        return "<span style='color:red'>待接单</span>";
                    case 2:
                        return "<span style='color:seagreen'>已接单</span>";
                    case 3:
                        return "<span style='color:seagreen'>配送中</span>";
                    case 4:
                        return "<span style='color:blue'>已完成</span>";
                    case 5:
                        return "<span style='color:blue'>已取消</span>";
                    default:
                        return "<span style='color:blue'>已关闭</span>";
                }
            });
            $grid->pay_status->display(function ($pay_status){
                switch ($pay_status){
//                支付状态（0未支付, 1已支付, 2已退款, 3部分退款 4免单）
                    case 1:
                        return "<span style='color:blue'>已支付</span>";
                    case 2:
                        return "<span style='color:seagreen'>已退款</span>";
                    case 3:
                        return "<span style='color:seagreen'>部分退款</span>";
                    case 4:
                        return "<span style='color:blue'>免单支付</span>";
                    default:
                        return "<span style='color:red'>未支付</span>";
                }
            });
            $grid->price;
            $grid->order_type->display(function ($order_type){
                switch ($order_type){
                    case 1:
                        return "<span style='color:blue'>代取</span>";
                    default:
                        return "<span style='color:blue'>代寄</span>";
                }
            });
            $grid->distribution_user_id->display(function ($user_id){
                $truename =  CoUser::where('id',$user_id)->value('truename');
                if($truename){
                    return $truename;
                }else{
                    return '无';
                }
            });
            $grid->created_at;
            $grid->updated_at;
            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('users.nickname','用户昵称');
                $filter->equal('school_id')->select(function (){
                    $CoSchool = new \App\Models\CoSchool();
                    $data =  $CoSchool::all()->where('status',1)->pluck('name', 'id');
                    return $data->toArray();
                });
                $filter->equal('order_type')->select(['1'=>'代取','2'=>'代寄']);
                $filter->equal('is_urgent','是否加急')->select(['1'=>'加急','2'=>'普通']);
                $filter->equal('status')->select(['0'=>'待支付','1'=>'待接单','2'=>'已接单','3'=>'配送中','4'=>'已完成','5'=>'已取消','6'=>'已关闭']);
                $filter->equal('pay_status')->select(['0'=>'待支付','1'=>'已支付','2'=>'已退款','3'=>'部分退款','4'=>'免单支付']);
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CoOrder(), function (Show $show) {
            $show->id;
            $show->order_no;
            $show->user_id->as(function ($user_id){
                return CoUser::where('id',$user_id)->value('nickname');
            });
            $show->school_id->as(function ($school_id){
                return CoSchool::where('id',$school_id)->value('name');
            });
            $show->distribution_type->as(function ($distribution_type){
               if($distribution_type ==1){
                   return '配送上楼';
               }else{
                   return '配送到楼下';
               }
            });
            $show->seat_time->as(function ($seat_time){
                $stop_time = $this->stop_time;
                return "{$seat_time} - {$stop_time}";
            });
            $show->status->as(function ($status){
                switch ($status){
                    case 0:
                        return "待支付";
                    case 1:
                        return "待接单";
                    case 2:
                        return "已接单";
                    case 3:
                        return "配送中";
                    case 4:
                        return "已完成";
                    case 5:
                        return "已取消";
                    default:
                        return "已关闭";
                }
            });

            $show->pay_status->as(function ($pay_status){
                switch ($pay_status){
                    case 1:
                        return "已支付";
                    case 2:
                        return "已退款";
                    case 3:
                        return "部分退款";
                    default:
                        return "未支付";
                }
            });

            $show->order_type->as(function ($order_type){
                switch ($order_type){
                    case 1:
                        return "代取";
                    default:
                        return "代寄";
                }
            });
            $show->is_urgent('是否加急')->as(function ($is_urgent){
                if($is_urgent == 1){
                    return "是";
                }else{
                    return "否";
                }
            });
            $show->price;
            $show->pay_price;
            $show->refund_price;
            $show->pay_time;

            $show->distribution_user_id;
            $show->distribution_user_id->as(function ($user_id){
                $truename =  CoUser::where('id',$user_id)->value('truename');
                if($truename){
                    return $truename;
                }else{
                    return '无';
                }
            });

            $show->confirm_time;
            $show->cancel_time;
            $show->collect_time;
            $show->close_time;
            $show->comments_time;
            $show->is_comments;
            $show->pick_imgs->explode(',')->image();
            $show->end_imgs->explode(',')->image();
            $show->safety_code;
            $show->remarks;
            $show->created_at;
            $show->updated_at;
            $show->disableDeleteButton();
            $show->disableEditButton();
            $show->取件送件地址(function ($model) {
                return Show::make($model->city_id, new CoUserCity(), function (Show $show) {
                    $show->name('名称');
                    $show->tel('电话');
                    $show->sex('性别')->as(function ($sex){
                        if($sex ==1){
                            return '男';
                        }else{
                            return '女';
                        }
                    });
                    $show->school_id('学校')->as(function ($school_id){
                        return CoSchool::where('id',$school_id)->value('name');
                    });
                    $show->school_building_id('楼栋名')->as(function ($id){
                        return \App\Models\CoSchoolBuilding::where('id',$id)->value('title');
                    });
                    $show->address('详细地址');
                    $show->disableDeleteButton();
                    $show->disableEditButton();
                });
            });

            $show->快递信息(function ($model) {
                $grid = new Grid(new \App\Admin\Repositories\CoOrderExpress());

                $grid->model()->where('order_no', $model->order_no);
                $grid->express_name('快递公司');
                $grid->cargo_name('快递类型');
                $grid->pick_code('取件码');
                $grid->pick_tel('电话');
                $grid->pick_method('备注');
                $grid->pick_address('取件地址');
                $grid->weight_name('重量');
                $grid->name('联系人名称');
                $grid->tel('联系人电话');
                $grid->sex('性别')->display(function ($sex){
                    if($sex ==1){
                        return '男';
                    }else{
                        return '女';
                    }
                });

                $grid->mergename('省市区');
                $grid->address('详细地址');
                $grid->disableDeleteButton();
                $grid->disableEditButton();
                $grid->disableCreateButton();
                return $grid;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CoOrder(), function (Form $form) {
            $form->display('id');
            $form->text('order_no');
            $form->text('user_id');
            $form->text('school_id');
            $form->text('user_note');
            $form->text('distribution_id');
            $form->text('seat_time');
            $form->text('stop_time');
            $form->text('status');
            $form->text('pay_status');
            $form->text('price');
            $form->text('pay_price');
            $form->text('refund_price');
            $form->text('order_type');
            $form->text('pay_time');
            $form->text('confirm_time');
            $form->text('cancel_time');
            $form->text('collect_time');
            $form->text('close_time');
            $form->text('comments_time');
            $form->text('is_comments');
            $form->text('is_delete_time');
            $form->text('address_id');
            $form->text('pick_imgs');
            $form->text('end_imgs');
            $form->text('safety_code');
            $form->text('distribution_user_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
